#!/usr/bin/python3
# -*- coding: utf-8 -*-

import numpy as np

def mk_trend_test(data):

	stat_s = 0
	slope = []

	for k in range(len(data) - 1):
		for j in range(k+1,len(data)):
			stat_s += np.sign(data[j]-data[k])
			slope.append((data[j]-data[k]) / (j - k))

	unique_data = np.unique(data)

	if len(data) == len(unique_data):
		var_s = (len(data) * (len(data) - 1) * (2 * len(data) + 5)) / 18.0
	else:
		tp = np.array([sum( data == value) for value in unique_data])
		var_s =(len(data) * (len(data) - 1) * (2 * len(data) + 5) - np.sum(tp * (tp - 1) * (2 * tp + 5))) / 18.0

	if var_s > 0:
		z_value = (stat_s - 1) / np.sqrt(var_s)
	elif var_s < 0:
		z_value = (stat_s + 1) / np.sqrt(var_s)
	else:
		z_value = 0


	return (np.median(slope), z_value)

if __name__ == '__main__':
	(slope, z_value) = mk_trend_test([4,3,5,7,9,7,8,10,9])
	print(slope)
	print(z_value)







